
local IMMessage = import(".IMMessage")

local fileName = string.getModeName(...)
local IMCommand = class(fileName)

local GV_ON_JOINROOM_SUCC = 1	--join room succ
local GV_ON_JOINROOM_TIMEOUT = 2	--join room timeout
local GV_ON_JOINROOM_SVR_ERR = 3  --communication with svr occur some err, such as err data recv from svr
local GV_ON_JOINROOM_UNKNOWN = 4  --reserved, our internal unknow err

local GV_ON_NET_ERR = 5  --net err,may be can't connect to network

local GV_ON_QUITROOM_SUCC = 6  --quitroom succ, if you have join room succ first, quit room will alway return succ

local GV_ON_MESSAGE_KEY_APPLIED_SUCC = 7  --apply message authkey succ
local GV_ON_MESSAGE_KEY_APPLIED_TIMEOUT = 8		--apply message authkey timeout
local GV_ON_MESSAGE_KEY_APPLIED_SVR_ERR = 9  --communication with svr occur some err, such as err data recv from svr
local GV_ON_MESSAGE_KEY_APPLIED_UNKNOWN = 10  --reserved, our internal unknow err

local GV_ON_UPLOAD_RECORD_DONE = 11  --upload record file succ
local GV_ON_UPLOAD_RECORD_ERROR = 12  --upload record file occur error
local GV_ON_DOWNLOAD_RECORD_DONE = 13	 --download record file succ
local GV_ON_DOWNLOAD_RECORD_ERROR = 14	--download record file occur error

local GV_ON_STT_SUCC = 15  -- speech to text successful
local GV_ON_STT_TIMEOUT = 16  -- speech to text with timeout
local GV_ON_STT_APIERR = 17  -- server's error

local GV_ON_PLAYFILE_DONE = 18  --the record file played end

local GV_ON_ROOM_OFFLINE = 19  -- Dropped from the room
local GV_ON_UNKNOWN = 20

function IMCommand:ctor()
	self._model = app:getInst("IMModel")
	self._ctrl = app:getInst("IMRoomController")
	self._recordCtrl = app:getInst("IMRecordController")

	--------------- gcloud voice ------------
	im.setDelegate("onPlayEnd", function (code, filePath)
		local succ = code == GV_ON_PLAYFILE_DONE
		-- print("===>IMAPI im.onPlayEnd, succ, filePath = ", succ, filePath)
		self._ctrl:onPlayEnd(succ, filePath)
		self._recordCtrl:onPlayEnd(succ, filePath)
	end)

	im.setDelegate("onUploadEnd", function(code, filePath, fileId)
		-- print("===>IMAPI im.onUploadEnd, code, path, fileId = ", code, filePath, fileId)
		self._model:onUploadEnd(code==GV_ON_UPLOAD_RECORD_DONE, filePath, fileId)
	end)

	im.setDelegate("onDownloadEnd", function (code, filePath, fileId)
		-- print("===>IMAPI im.onDownloadEnd, code, path, fileId = ", code, filePath, fileId)
		self._model:onDownloadEnd(code==GV_ON_DOWNLOAD_RECORD_DONE, filePath, fileId)
	end)
	
	im.setDelegate("onAudio2Text", function (code, fileId, result)
		-- print("===>IMAPI im.onAudio2Text, code, fileId, result = ", code, fileId, result)
		self._model:onAudio2Text(code==GV_ON_STT_SUCC, fileId, result)
	end)

	im.setDelegate("OnApplyMessageKey", function (code)
		-- print("===>IMAPI im.OnApplyMessageKey, code = ", code)
		im.isGotApplyKey = (code==GV_ON_MESSAGE_KEY_APPLIED_SUCC)
		if code == GV_ON_MESSAGE_KEY_APPLIED_TIMEOUT then
			self._model:applyMessageKey()
		end
	end)

	im.setDelegate("OnJoinRoom", function(code, room, memberId)
		if DEBUG > 1 then
			self._model:addSystemMsg("im.OnJoinRoom memberId = "..memberId)
		end
		-- print("===>IMAPI im.OnJoinRoom, code, room, memberId = ", code, room, memberId)
		self._recordCtrl:onJoinRoom(GV_ON_JOINROOM_SUCC==code, room, memberId)
		if code == GV_ON_JOINROOM_TIMEOUT then
			self._recordCtrl:joinTeamRoom()
		end
	end)
	
	im.setDelegate("OnQuitRoom", function(code, room)
		if DEBUG > 1 then
			self._model:addSystemMsg("OnQuitRoom")
		end
		-- print("===>IMAPI im.OnQuitRoom, code, room = ", code, room)
		self._recordCtrl:onQuitRoom(GV_ON_QUITROOM_SUCC==code, room)
	end)

	im.setDelegate("OnStatusUpdate", function(code, room, memberId)
		-- print("===>IMAPI im.OnStatusUpdate, code, room, memberId= ", code, room, memberId)
		if DEBUG > 1 then
			self._model:addSystemMsg("im.OnStatusUpdate, code=%s, room=%s, memberId=%s", {code, room, memberId})
		end
		if code == GV_ON_ROOM_OFFLINE then
			-- print("===>IMAPI Dropped from room", room, memberId)
			self._recordCtrl:joinTeamRoom(memberId)
		end
	end)
	
	-- status有“0”：停止说话 “1”：开始说话 “2”:继续说话
	im.setDelegate("OnMemberVoice", function(room, member, status)

	end)

end

return IMCommand